import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { environment } from '@env/environment';
import { StationService } from '../../station/service/station.service';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { SystemDictService } from '../../../system/dict/service/service.service';
import { StationEditComponent } from '../../station/edit/edit.component';
import { BaikeService } from '../service/baike.service';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BaikeEditComponent } from '../edit/edit.component';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less'],
})
export class BaikeListComponent implements OnInit {
  searchForm: any = FormGroup;
  dataList: any;
  statList: any;
  animalType: any;
  pageInfo = {
    pi: 1,
    ps: 10,
    total: 0,
    loading: false,
  };

  constructor(
    private fb: FormBuilder,
    private service: BaikeService,
    private notification: NzNotificationService,
    private modalService: NzModalService,
    private dictService: SystemDictService,
  ) {}

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      name: null,
    });
    this.dictService.dictList(environment.dict.animalType).subscribe((res) => {
      this.statList = res.data;
    });
    this.load();
  }

  add() {
    this.modalService
      .create({
        nzContent: BaikeEditComponent,
        nzComponentParams: {},
        nzTitle: '添加词条',
        nzFooter: null,
      })
      .afterClose.subscribe((res: any) => {
        this.load();
      });
  }

  edit(record: any) {
    this.modalService
      .create({
        nzContent: BaikeEditComponent,
        nzComponentParams: { record },
        nzTitle: '编辑词条',
        nzFooter: null,
      })
      .afterClose.subscribe((res: any) => {
        this.load();
      });
  }

  reset(): void {
    this.pageInfo.pi = 1;
    this.searchForm.reset();
    this.load();
  }

  search(): void {
    this.pageInfo.pi = 1;
    this.load();
  }

  load(): void {
    const params = {
      ...this.searchForm.value,
      animalType: this.animalType,
      current: this.pageInfo.pi,
      size: this.pageInfo.ps,
    };
    //删除没有的属性和空的
    for (let key in params) {
      if (params.hasOwnProperty(key) && (params[key] == null || params[key] == undefined || params[key] === ' ')) {
        delete params[key];
      }
    }
    this.pageInfo.loading = true;
    this.service.page(params).subscribe((res) => {
      this.dataList = res.data.records;
      this.pageInfo.total = res.data.total;
      this.pageInfo.loading = false;
    });
  }

  remove(id: string): void {
    this.service.remove(id).subscribe((res) => {
      this.load();
    });
  }

  setType(dictKey: number) {
    this.animalType = dictKey;
    this.load();
  }
}
